﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;

namespace MovieMetaDataFinder
{
    public static class TitleIO
    {
        public static Title GetDataFromXML(String FileName)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Title));
            try
            {
                TextReader tr = new StreamReader(FileName);
                Title title = (Title)xs.Deserialize(tr);
                tr.Close();
                return title;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }

        public static void SetDataFromXML(Title title, String fileName)
        {
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(Title));

                TextWriter tw = new StreamWriter(fileName);

                xs.Serialize(tw, title);

                tw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    [XmlInclude(typeof(PersonType))]
    [XmlInclude(typeof(Person))]
    [Serializable]
    public class Title
    {
        List<string> MasterGenres = new List<string>();

        public Title()
        {
            Studios = new List<string>();
            Persons = new List<Person>();
            GenreItems = new Genres();
            PopulateMasterGenres();
        }

        private void PopulateMasterGenres()
        {
            MasterGenres.Add("Action");
            MasterGenres.Add("Adventure");
            MasterGenres.Add("Animation");
            MasterGenres.Add("Biography");
            MasterGenres.Add("Comedy");
            MasterGenres.Add("Crime");
            MasterGenres.Add("Documentary");
            MasterGenres.Add("Drama");
            MasterGenres.Add("Family");
            MasterGenres.Add("Fantasy");
            MasterGenres.Add("Film-Noir");
            MasterGenres.Add("Game-Show");
            MasterGenres.Add("History");
            MasterGenres.Add("Horror");
            MasterGenres.Add("Music");
            MasterGenres.Add("Musical");
            MasterGenres.Add("Mystery");
            MasterGenres.Add("News");
            MasterGenres.Add("Reality-TV");
            MasterGenres.Add("Romance");
            MasterGenres.Add("Sci-Fi");
            MasterGenres.Add("Short");
            MasterGenres.Add("Sport");
            MasterGenres.Add("Talk-Show");
            MasterGenres.Add("Thriller");
            MasterGenres.Add("War");
            MasterGenres.Add("Western");
        }

        public decimal IMDBrating { get; set; }
        public string MPAARating { get; set; }
        public string ASIN { get; set; }
        public int TheMovieDBID { get; set; }
        public string IMDBID { get; set; }

        [XmlElement("Genres")]
        public Genres GenreItems { get; set; }

        public class Genres
        {
            public Genres()
            {
                Genre = new List<string>();
            }

            [XmlElement("Genre")]
            public List<string> Genre { get; set; }
        }


        public List<string> AddGenre(string value)
        {
            foreach (string item in this.MasterGenres)
            {
                if (value.ToLower().Contains(item.ToLower()))
                {
                    bool okToAdd = true;
                    foreach (string currentlyPlacedGenres in GenreItems.Genre)
                    {
                        if (item.ToLower() == currentlyPlacedGenres.ToLower())
                            okToAdd = false;
                    }
                    if (okToAdd) GenreItems.Genre.Add(item);
                }
            }
            return null;
        }

        public string LocalTitle { get; set; }
        public string OriginalTitle { get; set; }
        public string SortTitle { get; set; }
        public string AspectRatio { get; set; }
        public string ProductionYear { get; set; }
        public DateTime ReleaseDate { get; set; }
        public int RunningTime { get; set; }
        public string Description { get; set; }
        public List<string> Studios { get; set; }
        public string BackdropURL { get; set; }

        public enum PersonType
        {
            Actor, Director, Writer
        }

        public List<Person> Persons { get; set; }
        public class Person
        {
            public string Name { get; set; }
            public PersonType Type { get; set; }
            public string Role { get; set; }

            public Person(string name, PersonType personType, string role)
            {
                this.Name = name;
                this.Type = personType;
                this.Role = role;
            }

            public Person()
            {
            }
        }

        public string Barcode { get; set; }
    }
}
